﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using LightWeb.TransferModel;
using LightWeb.Respository;
using LightWeb.DomainModel;

namespace LightWeb.Blog
{
    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                LoadArticalList();
            }
        }

        private void LoadArticalList()
        {
            List<TmArcitalSummary> summary = new List<TmArcitalSummary>();

            using (OurTransactionScope ts = new OurTransactionScope())
            {
                var context = DataContext.Current;
                var list = context.Read<ArticalInfo>(1, 15, null, null).ModelList;
                foreach (ArticalInfo item in list)
                {
                    summary.Add(ModelConvert.FromDomainModel(item));
                }
            }
            this.repArticalList.DataSource = summary;
            this.repArticalList.DataBind();

        }
    }

    public class ModelConvert
    {
        public static TmArcitalSummary FromDomainModel(ArticalInfo articalInfo)
        {
            TmArcitalSummary obj = new TmArcitalSummary();
            obj.ArticalId = articalInfo.ArticalId;
            obj.PostDate = articalInfo.PostDate.ToString();
            obj.Summary = articalInfo.Summary;
            obj.Title = articalInfo.Title;
            return obj;
        }

        public static TmArticalComment FromDomainModel(ArticalComment entity)
        {
            TmArticalComment dto = new TmArticalComment() { ArticalId = entity.ArticalId, Name = entity.Name, Email = entity.Email, Site = entity.Site, Date = entity.Date.ToString(), Content = entity.Content };
            return dto;
        }
    }


}
